import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '../store/auth'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    name: 'dashboard',
    component: () => import('../views/Dashboard.vue'),
    meta: { title: '仪表盘', requiresAuth: true }
  },
  {
    path: '/wallet',
    name: 'wallet',
    component: () => import('../views/Wallet.vue'),
    meta: { title: '我的钱包', requiresAuth: true }
  },
  {
    path: '/deposit',
    name: 'deposit',
    component: () => import('../views/Deposit.vue'),
    meta: { title: '充值', requiresAuth: true }
  },
  {
    path: '/transfer',
    name: 'transfer',
    component: () => import('../views/Transfer.vue'),
    meta: { title: '转账', requiresAuth: true }
  },
  {
    path: '/transactions',
    name: 'transactions',
    component: () => import('../views/Transactions.vue'),
    meta: { title: '交易记录', requiresAuth: true }
  },
  {
    path: '/withdraw',
    name: 'withdraw',
    component: () => import('../views/Withdraw.vue'),
    meta: { title: '提现', requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('../views/Profile.vue'),
    meta: { title: '个人中心', requiresAuth: true }
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('../views/Settings.vue'),
    meta: { title: '设置', requiresAuth: true }
  },
  {
    path: '/c2c',
    name: 'c2c',
    component: () => import('../views/C2C.vue'),
    meta: { title: 'C2C交易', requiresAuth: true }
  },
  {
    path: '/c2c/publish',
    name: 'c2c-publish',
    component: () => import('../views/C2CPublish.vue'),
    meta: { title: '发布订单', requiresAuth: true }
  },
  {
    path: '/c2c/my-orders',
    name: 'c2c-my-orders',
    component: () => import('../views/C2CMyOrders.vue'),
    meta: { title: '我的订单', requiresAuth: true }
  },
  {
    path: '/c2c/my-trades',
    name: 'c2c-my-trades',
    component: () => import('../views/C2CMyTrades.vue'),
    meta: { title: '我的交易', requiresAuth: true }
  },
  {
    path: '/identity',
    name: 'identity',
    component: () => import('../views/IdentityVerification.vue'),
    meta: { title: '实名认证', requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
  } else if ((to.path === '/login' || to.path === '/register') && authStore.isAuthenticated) {
    next('/dashboard')
  } else {
    next()
  }
})

router.afterEach((to) => {
  document.title = `${to.meta.title || '钱包'} - 数字货币钱包`
})

export default router

